package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService{
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

    //设置套餐和检查组多对多关系，操作t_setmeal_checkgroup
    public void setSetmealAndCheckgroup(Integer setmealId,Integer[] checkgroupIds){
        if(checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
    public void add(Setmeal setmeal,Integer[]checkgroupIds){
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckgroup(setmealId,checkgroupIds);
        String fileName = setmeal.getImg();
        Jedis jedis = jedisPool.getResource();
        jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
        jedis.close();

    }

    public PageResult pageQuery(QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    //根据套餐ID查询套餐详情（套餐基本信息、套餐对应的检查组信息、检查组对应的检查项信息）
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
//        return getById(id);
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        Setmeal oldSetmeal = setmealDao.findById(setmeal.getId());
        String oldImg = oldSetmeal.getImg();
        setmealDao.edit(setmeal);
        setmealDao.deleteAssocication(setmeal.getId());
        this.setSetmealAndCheckgroup(setmeal.getId(), checkgroupIds);
    }

    @Override
    public void delete(Integer setmealId) {
        long count = setmealDao.findCheckGroupCountBysetmealId(setmealId);
        if (count > 0) {
            throw new RuntimeException();
        }
        setmealDao.deleteById(setmealId);
    }

    @Override
    public List<Integer> findCheckgroupIdsBySetmealId(Integer setmealId) {
        return setmealDao.findCheckgroupIdsBySetmealId(setmealId);
    }

    //查询套餐预约占比数据
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }



    @Autowired
    private CheckGroupDao checkgroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    private Setmeal getById(int id) {
        Setmeal setmeal = setmealDao.getById(id);
        int[] ids = new int[]{id};
        for(int setmealId:ids) {//setmealDao.xml里面的collection标签
            List<CheckGroup> checkGroups = checkgroupDao.listBySetmealId(setmealId);
            for (CheckGroup group : checkGroups) { //checkgroupDao.xml里面的collection标签
                List<CheckItem> checkItems = checkItemDao.findCheckItemByCheckGroupId(group.getId());
                group.setCheckItems(checkItems);
            }
            setmeal.setCheckGroups(checkGroups);
        }
        return setmeal;
    }
}
